package com.tongfusoft.document.web.controller.persionCenter;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.Docs;
import com.tongfusoft.document.entity.InputProfiles;
import com.tongfusoft.document.entity.UserFavorites;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.dictionary.service.IDocumentDictionaryService;
import com.tongfusoft.document.personalCenter.myFavorite.service.IMyFavoriteService;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;

@Controller
public class MyFavoriteController extends BaseController{
	
	@Autowired
	@Qualifier("iMyFavoriteService")
	public IMyFavoriteService iMyFavoriteService;
	
	
	@RequestMapping("/personalCenter/myFavorite")
	@UrlDesc(value = "我的收藏页")
	public String myCollection(Model model){
		
		model.addAttribute("mainPageKey", "myFavorite");
		model.addAttribute("mainPage", "personalCenter/myFavorite");
		return "base";
	}
	
	@RequestMapping("/myFavorite/query")
	@UrlDesc(value = "我的收藏查询")
	@ResponseBody
	public PageInfo systemParamQuery(Model model, int pageIndex, int pageSize,String query) {
		PageInfo pageInfo = new PageInfo();
		try {
			UserFavorites userFavorites =new UserFavorites();
			if(StringUtils.isNotEmpty(query)){
				Map queryMap = JsonUtils.getMap4Json(query);
				if(queryMap.get("fastText")!=null){
					
					userFavorites.setDocTitle(queryMap.get("fastText").toString());
				}
			}
			pageInfo = this.iMyFavoriteService.favoriteList(userFavorites, (Users) super.getUser(), pageSize, pageIndex);

		} catch (Exception e) {

			e.printStackTrace();
		}
		return pageInfo;

	}
	
	@RequestMapping("/myFavorite/del")
	@UrlDesc(value = "删除我的收藏")
	@ResponseBody
	public ResultModel del(HttpServletRequest request,UserFavorites favorites) {
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			this.iMyFavoriteService.deleteUserFavorites(favorites);
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
